﻿module Sustainalytics.Event {

    import Common = Sustainalytics.Common;

    // EventViewController controller
    export class EventViewController {
        //public eventsCurrentDate: EventDto[];
        //public currentUTCDate: Date;
        //public startDate: string;
        //public disablePreviousButton: boolean;
        public orderBy: string[];
        public orderAscending: boolean;
        public $routeParams: any;
        public event: EventDto;

        //Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private RoutingService: Routing.IRoutingService,
            private EventService: EventService,
            private notificationFactory: Common.INotificationFactory,
            private $window: ng.IWindowService,
            private $sce: ng.ISCEService) {

            var ctx = this;
            ctx.$routeParams = ctx.RoutingService.getRouteParams();
            $scope['eventView'] = ctx;

            ctx.getEventById();

            ////#region events listed in calendar
            //var date = new Date();
            //ctx.currentUTCDate = ctx.toUTCDate(date);

            //var curentDate: string = ctx.$routeParams['eventsDate'];
            //curentDate = curentDate.replace("\"", "");
            //curentDate = curentDate.substring(0, 10);

            //var dateForDisablePrevButton = new Date(curentDate);
            //dateForDisablePrevButton.setDate(dateForDisablePrevButton.getDate() - 1);

            //ctx.disablePreviousButton = true;

            //var diffMonth = ctx.monthDiff(ctx.currentUTCDate, dateForDisablePrevButton);

            //if (diffMonth < -2) {
            //    ctx.disablePreviousButton = true;
            //}
            //else {
            //    ctx.disablePreviousButton = false;
            //}

            //ctx.getEventsByDate(curentDate);
            ////#endregion events listed in calendar
        }

        public getEventById(): ng.IPromise<EventDto> {
            var ctx = this;
            ctx.$rootScope['loading'] = true;

            // get event ID
            var eventId: string = ctx.$routeParams['eventId'];

            // get news by id
            var promiseGet: ng.IPromise<EventDto> = ctx.EventService.getEventById(eventId);

            //on success
            promiseGet
                .then(function (data) {
                ctx.event = data;
            })
            ['finally'](function () {
                ctx.$rootScope['loading'] = false;
            });
            return promiseGet;
        }

        public RenderHTML(htmlCode: string) {
            var ctx = this;
            return ctx.$sce.trustAsHtml(htmlCode);
        }

        ////#region events listed in calendar
        //public toUTCDate = function (date) {
        //    var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
        //    return _utc;
        //};

        //public getEventsByDate(startDate: string): ng.IPromise<EventDto> {
        //    var ctx = this;
        //    ctx.$rootScope['loading'] = true;

        //    ctx.startDate = startDate;

        //    // get event by id
        //    var promiseGet: ng.IPromise<EventDto> = ctx.EventService.getEventByDate(ctx.startDate);

        //    //on success
        //    promiseGet
        //        .then(function (lst) {
        //            ctx.eventsCurrentDate = lst["items"];
        //        })
        //        ['finally'](function () {
        //            ctx.$rootScope['loading'] = false;
        //        });
        //    return promiseGet;
        //}

        //public getNextDate() {
        //    var ctx = this;

        //    //convert string to date
        //    var currentDate = ctx.startDate.substring(0,10);
        //    var newDate = new Date(currentDate);

        //    newDate.setDate(newDate.getDate() + 1);

        //    ctx.startDate = newDate.getFullYear() + "-" + ('0' + (newDate.getMonth() + 1)).slice(-2) + "-" + ('0' + newDate.getDate()).slice(-2);

        //    ctx.$routeParams['eventsDate'] = ctx.startDate;
        //    ctx.$location.path.apply(ctx.$location, ["events/date/" + ctx.startDate]);
        //    ctx.getEventsByDate(ctx.startDate);
        //}

        //public getPreviousDate() {
        //    var ctx = this;

        //    //convert string to date
        //    var currentDate = ctx.startDate.substring(0, 10);
        //    var newDate = new Date(currentDate);

        //    newDate.setDate(newDate.getDate() - 1);

        //    ctx.startDate = newDate.getFullYear() + "-" + ('0' + (newDate.getMonth() + 1)).slice(-2) + "-" + ('0' + newDate.getDate()).slice(-2);

        //    ctx.$routeParams['eventsDate'] = ctx.startDate;
        //    ctx.$location.path.apply(ctx.$location, ["events/date/" + ctx.startDate]);
        //    ctx.getEventsByDate(ctx.startDate);
        //}

        //public monthDiff(d1, d2) {
        //    var months;
        //    months = (d2.getFullYear() - d1.getFullYear()) * 12;
        //    months -= d1.getMonth();
        //    months += d2.getMonth();
        //    return months <= 0 ? months : months;
        //}
        ////#endregion events listed in calendar
    }

    // register controller 
    angular.module('Sustainalytics.Event').controller('EventViewController',
        ['$rootScope', '$scope', '$q', '$location', 'RoutingService', 'EventService', 'notificationFactory', '$window', '$sce', EventViewController]);
}
 